﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TARS.User
{
    public partial class updateInfoUser : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            using (var db = new TARSDelivery_EprojectEntities())
            {
                //load data form IDUser
                Guid userId = new Guid(Session["IDUser"].ToString());
                var loadInfo = db.t_EmployeeInfo.Where(x => x.UserID == userId);
                foreach (var user in loadInfo)
                {
                    txtSSN.Text = user.Emp_SSN.ToString();
                    txtAddress.Text = user.Emp_Address.ToString();
                    txtFName.Text = user.Emp_FName.ToString();
                    txtLName.Text = user.Emp_LName.ToString();
                    txtCity.Text = user.Emp_City.ToString();
                    txtState.Text = user.Emp_State.ToString();
                    txtZipcode.Text = user.Emp_ZipCode.ToString();
                    raddob.SelectedDate = Convert.ToDateTime(user.Emp_DOB.ToString());
                }

            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            using (var db = new TARSDelivery_EprojectEntities())
            {
                //set park
                string userName="";
                DateTime dob = Convert.ToDateTime(raddob.SelectedDate.ToString());
                DateTime updateTime = Convert.ToDateTime(DateTime.Now.ToString());
                Guid userID = new Guid(Session["IDUser"].ToString());
                var check = db.t_updateEmployee.Where(x => x.userID == userID);
                var loadUser = db.t_User.Where(x => x.Username == txtUser.Text);
                var loadOPass = db.t_User.Where(x => x.Password == txtOPass.Text);
                foreach(var user in loadUser)
                {
                    userName = user.Username.ToString();
                }
                //Check old password 
                if (loadOPass.Count() < 1)
                {
                    lblInfo.Visible = false;
                    lblUser.Visible = false;
                    lblOldPass.Visible = true;
                }
                else
                    // check the existence of username 
                    if (loadUser.Count()>0 && userName!=txtUser.Text)
                    {
                        lblOldPass.Visible = false;
                        lblUser.Visible = true;
                        lblInfo.Visible = false;
                    }
                    else
                    {
                        //update user password 
                        var update = db.Update_User(userID, txtUser.Text, txtNPass.Text);
                    }
                //check the existence of IDuser in table update 
                if (check.Count() > 0)
                {
                    lblInfo.Visible = true;
                }else
                {
                     var updateInfo = db.Insert_updateEmployee(userID, txtFName.Text, txtLName.Text
                                , txtAddress.Text, txtCity.Text, txtState.Text,
                                txtZipcode.Text, txtSSN.Text, dob, updateTime);
                }
                   
            }
        }
    }
}